﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HousingDistribution
{
    partial class FormPersonDetailsNoHousingTab
    {
        //选房窗口调用的回调函数
        public delegate void UpdatePersonHousingInfoDelegate(PersonInfo personInfo);
        public void UpdatePersonHousingInfo(PersonInfo personInfo)
        {
            this.curPerInfo = personInfo;
            UpdatePersonHousingInfo();
        }

        private void UpdatePersonHousingInfo()
        {
            DisplayCurHousing(this.curPerInfo.housingInfo);

            //每次修改数据并不是重新读取数据库，因此需要更新内存中的DataTable保持一致
            int index = this.curIndex % MAX_COUNT;
            UpdateDataTable(ref this.personTable, index, this.curPerInfo);
        }

        #region 数据更新

        //当修改界面的值之后，需要更新当前内存中的personTable
        private void UpdateDataTable(ref DataTable dt, int rowIndex, PersonInfo personInfo)
        {
            DataRow dr = dt.Rows[rowIndex];

            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.NAME)] = personInfo.name;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TYPE)] = (int)personInfo.type;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SEX)] = personInfo.sex;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.UNIT)] = personInfo.unit;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY)] = personInfo.adminDuty;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.ADMIN_DUTY_TIME)] = personInfo.adminDutyTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK)] = personInfo.techRank;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_RANK_TIME)] = personInfo.techRankTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE)] = personInfo.techTitle;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.TECH_TITLE_TIME)] = personInfo.techTitleTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.CUR_RANK_BEFORE_TIME)] = personInfo.curRankBeforeTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_TIME)] = personInfo.workTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SCHOOL_TIME)] = personInfo.schoolTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SIGNUP_TIME)] = personInfo.signupTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.SERVICE_LENGTH)] = ServiceLengthCount.Instance().ServiceLength(personInfo);
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.RETIREMENT_TIME)] = personInfo.retirementTime;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_NAME)] = personInfo.mateInfo.name;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_RANK)] = personInfo.mateInfo.rank;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MATE_UNIT)] = personInfo.mateInfo.unit;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.REMARKS)] = personInfo.remarks;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_ELIGIBLE)] = personInfo.housingEligible;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.BOTH_EMPLOYEE)] = personInfo.bothEmployee;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.EVALUATION)] = personInfo.evaluation;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSE_ADDR)] = personInfo.curHousingAddr.addr;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSE_AREA)] = personInfo.curHousingAddr.area;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.HOUSING_INFO)] = this.sqlCreator.GetHousingInfoString(personInfo.housingInfo);
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.FORMER_HOUSING_ID)] = this.sqlCreator.GetFormerHousingIdString(personInfo.formerHousingId);
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WORK_STATUS)] = (int)personInfo.workStatus;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.MARITAL)] = (int)personInfo.marital;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.WITHARMY)] = (int)personInfo.withArmy;

            int bothEmpFlag = this.checkBoxBothEmp.Checked ? 1 : 0;
            dr[DataDictionary.Instance().PersonTableFieldName(PersonColConsts.BOTH_EMPLOYEE)] = bothEmpFlag;
        }
        #endregion

        #region 人员信息界面相关
        private void DisplayInfo(int curIndex)
        {
            if (curIndex < 0 || curIndex >= personCount)
                return;

            try
            {
                int curOffset = curIndex / MAX_COUNT;

                //personTable 无效，需重新读取
                if (offset != curOffset || this.personTable == null)
                {
                    ResetPersonDisplayState();

                    offset = curOffset;
                    //读取对应区间的数据
                    this.personTable = ReadPersonData(offset * MAX_COUNT);
                }

                if (this.personTable.Rows.Count == 0)
                    return;

                int index = curIndex % MAX_COUNT;

                this.curPerInfo = this.parser.ParsePerson(this.personTable.Rows[index]);
                
                DisplayPerson(this.curPerInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //curPerInfo
        public void DisplayPerson(PersonInfo personInfo)
        {
            this.curPerInfo = personInfo;
            DisplayPersonInfo(this.curPerInfo);

            //显示当前住房信息
            DisplayCurHousing(this.curPerInfo.housingInfo);
        }

        private void DisplayPersonInfo(PersonInfo personInfo)
        {
            if (personInfo.type == PersonTypeEnum.ArmMan)
            {
                ResetContrlsBesidesTab();
                ArmmanInfo(personInfo);

                //清空其他tab的无效信息
                ResetStaffInfoTab();
                ResetNCOInfoTab();
                ResetWorkerInfoTab();

                this.tabControl.SelectedIndex = 0;
            }
            else if (personInfo.type == PersonTypeEnum.Staff)
            {
                ResetContrlsBesidesTab();
                StaffInfo(personInfo);

                ResetArmmanInfoTab();
                ResetNCOInfoTab();
                ResetWorkerInfoTab();

                this.tabControl.SelectedIndex = 1;
            }
            else if (personInfo.type == PersonTypeEnum.Worker)
            {
                ResetContrlsBesidesTab();
                WorkerInfo(personInfo);

                ResetArmmanInfoTab();
                ResetStaffInfoTab();
                ResetNCOInfoTab();

                this.tabControl.SelectedIndex = 3;
            }
            else if (personInfo.type == PersonTypeEnum.NCO)
            {
                ResetContrlsBesidesTab();
                NCOInfo(personInfo);

                ResetArmmanInfoTab();
                ResetStaffInfoTab();
                ResetWorkerInfoTab();

                this.tabControl.SelectedIndex = 2;
            }
            else
            {
            }
        }
        
        //显示当前住房信息（this.dataGridViewCurHousing）
        private void DisplayCurHousing(List<HousingOccupancyInfo> housingInfo)
        {
            //housingInfo.Count > 0 表明有院内住房
            if (housingInfo != null && housingInfo.Count > 0)
            {
                try
                {
                    string idhousingColName = DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID);
                    string checkinTimeColName = DataDictionary.Instance().DGVNewColName(DGVNewColConsts.CHECKINTIME);

                    StringBuilder sql = new StringBuilder("select idhousing , housingType, housingFullAddr, housingArea, housingUse, housingProperty from housing where ");
                    sql.Append(this.sqlCreator.GetHousingInfoQueryString(housingInfo));

                    DataTable dt = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

                    //dt需要增加一列保存checkinTime
                    dt.Columns.Add(checkinTimeColName, typeof(string));

                    for (int i = 0; i < dt.Rows.Count;i++ )
                    {
                        //首先获取idhousing
                        int id = (int)dt.Rows[i][idhousingColName];
                        foreach (HousingOccupancyInfo info in housingInfo)
                        {
                            if (info.id == id)
                            {
                                //获取checkinTime加到datagridview中（时间有效）
                                if (info.checkinTime.CompareTo(DateProc.InvalidDate()) != 0)
                                    dt.Rows[i][checkinTimeColName] = info.checkinTime.ToShortDateString();
                                
                                break;
                            }
                        }
                    }

                    this.dataGridViewCurHousing.DataSource = dt;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
            else
                this.dataGridViewCurHousing.DataSource = null;
        }

        //格式化显示时间，无效时间不显示，有效时间为YYYY/MM/DD
        private string DateTimeDisplayFormat(DateTime date)
        {
            if (date.CompareTo(DateProc.InvalidDate()) == 0)
                return null;
            else
                return date.ToShortDateString();
        }

        //界面共享区域信息显示
        void PersonSharingInfo(PersonInfo personInfo)
        {
            textBoxMateName.Text = personInfo.mateInfo.name;
            textBoxMateRank.Text = personInfo.mateInfo.rank;
            textBoxMateUnit.Text = personInfo.mateInfo.unit;

            this.checkBoxHousingEligible.Checked = (personInfo.housingEligible == 1);
            this.checkBoxBothEmp.Checked = (personInfo.bothEmployee == 1);

            string marital = DataDictionary.Instance().GetMaritalStatus(personInfo.marital);
            this.comboBoxMarital.Text = marital;

            CtrlControlsStatusByMarital(personInfo.marital);

            textBoxCurAddr.Text = personInfo.curHousingAddr.addr;
            textBoxRemarks.Text = personInfo.remarks;

            //有备注信息
            //if (personInfo.type == PersonTypeEnum.ArmMan && personInfo.remarks != "" && personInfo.withArmy!= WithArmyStatusEnum.Unkown)
            //{
            //    textBoxRemarks.Text = personInfo.remarks + "," + DataDictionary.Instance().GetWitharmyStatus(personInfo.withArmy);
            //}
            //else
            //    textBoxRemarks.Text = personInfo.remarks;

            //textBoxFormerHousingInfo.Text = this.sqlCreator.GetFormerHousingIdString(personInfo.formerHousingId);
        }
        //界面共享区域信息获取
        void GetPersonSharingInfo(ref PersonInfo personInfo)
        {
            PersonMaritalEnum marital;
            if (this.comboBoxMarital.SelectedItem == null)
            {
                marital = PersonMaritalEnum.Unknown;
            }
            else
            {
                marital = DataDictionary.Instance().GetMorital(this.comboBoxMarital.SelectedItem.ToString());
                personInfo.marital = marital;
            }

            if (marital == PersonMaritalEnum.Married || marital == PersonMaritalEnum.Widowed)
            {
                personInfo.mateInfo.name = textBoxMateName.Text;
                personInfo.mateInfo.rank = textBoxMateRank.Text;
                personInfo.mateInfo.unit = textBoxMateUnit.Text;
            }
            else
            {
                personInfo.mateInfo.name = null;
                personInfo.mateInfo.rank = null;
                personInfo.mateInfo.unit = null;
            }

            personInfo.bothEmployee = this.checkBoxBothEmp.Checked ? 1 : 0;
            personInfo.housingEligible = this.checkBoxHousingEligible.Checked ? 1 : 0;

            personInfo.remarks = textBoxRemarks.Text;
            personInfo.curHousingAddr.addr = textBoxCurAddr.Text;

            if (personInfo.curHousingAddr.addr == "" || personInfo.curHousingAddr.addr == null)
                personInfo.curHousingAddr.area = null;
            
        }

        void ResetContrlsBesidesTab()
        {
            this.textBoxMateName.Text = null;
            this.textBoxMateRank.Text = null;
            this.textBoxMateUnit.Text = null;
            this.checkBoxBothEmp.Checked = false;
            this.checkBoxHousingEligible.Checked = false;
            this.textBoxRemarks.Text = null;

            this.comboBoxMarital.Text = null;
            this.textBoxCurAddr.Text = null;

            this.dataGridViewCurHousing.DataSource = null;
        }

        //军人干部
        private void ArmmanInfo(PersonInfo personInfo)
        {
            textBoxArmManName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnArmManMale.Checked = true;
            else
                radioBtnArmManFemale.Checked = true;

            comboBoxArmManUnit.Text = personInfo.unit;
            comboBoxArmManAdminDuty.Text = personInfo.adminDuty;
            textBoxArmManAdminDutyTime.Text = DateTimeDisplayFormat(personInfo.adminDutyTime);
            comboBoxArmManTechRank.Text = personInfo.techRank;
            textBoxArmManTechRankTime.Text = DateTimeDisplayFormat(personInfo.techRankTime);
            comboBoxArmManTechTitle.Text = personInfo.techTitle;
            textBoxArmManTechTitleTime.Text = DateTimeDisplayFormat(personInfo.techTitleTime);
            textBoxArmManCurRankBeforeTime.Text = DateTimeDisplayFormat(personInfo.curRankBeforeTime);
            textBoxArmManWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxArmManSchoolTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxArmManSignupTime.Text = DateTimeDisplayFormat(personInfo.signupTime);
            comboBoxArmmanEvaluation.Text = personInfo.evaluation;

            textBoxArmManRetirementTime.Text = DateTimeDisplayFormat(personInfo.retirementTime);

            string otherType = DataDictionary.Instance().GetWorkStatus(personInfo.workStatus);
            comboBoxArmmanOtherType.Text = otherType;

            string withArmy = DataDictionary.Instance().GetWitharmyStatus(personInfo.withArmy);
            this.comboBoxArmmanWitharmy.Text = withArmy;

            //转业复原人员6当年能分,离退休2、调出3、除名4、遗属5不分
            if ((personInfo.workStatus == WorkStatusEnum.Demobilization && (HousingDisDeadline.Instance().Deadline().Year - personInfo.retirementTime.Year != 0)) 
                || (personInfo.workStatus == WorkStatusEnum.Fired || personInfo.workStatus == WorkStatusEnum.Martyred 
                || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Transferred 
                || (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)))
            {
                personInfo.housingEligible = 0;
            }

            //显示detail退休时间文本，只显示不用于程序处理
            if ((int)personInfo.workStatus == 6)
                labelRetirementTime.Text = "转业时间";
            else if ((int)personInfo.workStatus == 3)
                labelRetirementTime.Text = "调出时间";
            else if ((int)personInfo.workStatus == 4)
                labelRetirementTime.Text = "除名时间";
            else
                labelRetirementTime.Text = "退休时间";


            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            PersonSharingInfo(personInfo);
        }

        private void ResetArmmanInfoTab()
        {
            textBoxArmManName.Text = null;
            radioBtnArmManMale.Checked = false;
            radioBtnArmManFemale.Checked = false;

            comboBoxArmManUnit.Text = null;
            comboBoxArmManAdminDuty.Text = null;
            textBoxArmManAdminDutyTime.Text = null;
            comboBoxArmManTechRank.Text = null;
            textBoxArmManTechRankTime.Text = null;
            comboBoxArmManTechTitle.Text = null;
            textBoxArmManTechTitleTime.Text = null;
            textBoxArmManCurRankBeforeTime.Text = null;
            textBoxArmManWorkTime.Text = null;
            textBoxArmManSchoolTime.Text = null;
            textBoxArmManSignupTime.Text = null;
            comboBoxArmmanEvaluation.Text = null;

            textBoxArmManRetirementTime.Text = null;
            comboBoxArmmanOtherType.Text = null;
            comboBoxArmmanWitharmy.Text = null;
        }
        private void GetValueFromArmman(ref PersonInfo personInfo)
        {
            personInfo.name = textBoxArmManName.Text;

            if (radioBtnArmManMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxArmManUnit.Text;
            personInfo.adminDuty = comboBoxArmManAdminDuty.Text;
            personInfo.adminDutyTime = this.dateProc.Parse(textBoxArmManAdminDutyTime.Text);
            personInfo.techRank = comboBoxArmManTechRank.Text;
            personInfo.techRankTime = this.dateProc.Parse(textBoxArmManTechRankTime.Text);
            personInfo.techTitle = comboBoxArmManTechTitle.Text;
            personInfo.techTitleTime = this.dateProc.Parse(textBoxArmManTechTitleTime.Text);
            personInfo.curRankBeforeTime = this.dateProc.Parse(textBoxArmManCurRankBeforeTime.Text);
            personInfo.workTime = this.dateProc.Parse(textBoxArmManWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxArmManSchoolTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxArmManSignupTime.Text);
            personInfo.evaluation = comboBoxArmmanEvaluation.Text;

            personInfo.retirementTime = this.dateProc.Parse(textBoxArmManRetirementTime.Text);

            WorkStatusEnum otherType;
            if (this.comboBoxArmmanOtherType.SelectedItem == null)
            {
                otherType = WorkStatusEnum.Unknown;
            }
            else
            {
                otherType = DataDictionary.Instance().GetWorkStatus(this.comboBoxArmmanOtherType.SelectedItem.ToString());
                personInfo.workStatus = otherType;
            }

            WithArmyStatusEnum withArmy;
            if (this.comboBoxArmmanWitharmy.SelectedItem == null)
            {
                withArmy = WithArmyStatusEnum.Unkown;
            }
            else
            {
                withArmy = DataDictionary.Instance().GetWitharmy(this.comboBoxArmmanWitharmy.SelectedItem.ToString());
                personInfo.withArmy = withArmy;
            }

            //转业复原人员当年能分,离退休、调出、除名、遗属不分
            if ((personInfo.workStatus == WorkStatusEnum.Demobilization && (HousingDisDeadline.Instance().Deadline().Year - personInfo.retirementTime.Year != 0))
                || (personInfo.workStatus == WorkStatusEnum.Fired || personInfo.workStatus == WorkStatusEnum.Martyred
                || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Transferred))
            {
                personInfo.housingEligible = 0;
                checkBoxHousingEligible.Checked = false;
            }

            if (personInfo.retirementTime != DateProc.InvalidDate())
            {
                personInfo.housingEligible = 0;
                checkBoxHousingEligible.Checked = false;
            }

            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;
            else
                personInfo.housingEligible = 0;

            GetPersonSharingInfo(ref personInfo);
        }

        //高级士官
        private void NCOInfo(PersonInfo personInfo)
        {
            textBoxNCOName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnNCOMale.Checked = true;
            else
                radioBtnNCOFemale.Checked = true;

            comboBoxNCOUnit.Text = personInfo.unit;
            textBoNCOTechTitle.Text = personInfo.techTitle;
            textBoxNCOTechTitleTime.Text = DateTimeDisplayFormat(personInfo.techTitleTime);
            textBoxNCOWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxNCOSignupTime.Text = DateTimeDisplayFormat(personInfo.signupTime);

            textBoxNCORetirementTime.Text = DateTimeDisplayFormat(personInfo.retirementTime);

            int serviceLength = ServiceLengthCount.Instance().ServiceLength(personInfo);
            if (serviceLength < 0)
                serviceLength = 0;

            textBoxNCOServiceLength.Text = serviceLength.ToString();

            string otherType = DataDictionary.Instance().GetWorkStatus(personInfo.workStatus);
            comboBoxNCOOtherType.Text = otherType;

            //转业复原人员6当年能分,离退休、调出、除名、遗属不分
            if ((personInfo.workStatus == WorkStatusEnum.Demobilization && (HousingDisDeadline.Instance().Deadline().Year - personInfo.retirementTime.Year != 0)) 
                || (personInfo.workStatus == WorkStatusEnum.Fired || personInfo.workStatus == WorkStatusEnum.Martyred 
                || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Transferred))
            {
                personInfo.housingEligible = 0;
            }

            //显示detail退休时间文本，只显示不用于程序处理
            if (personInfo.workStatus == WorkStatusEnum.Demobilization)
                labeNCOretirementTime.Text = "转业时间";
            else if (personInfo.workStatus == WorkStatusEnum.Transferred)
                labeNCOretirementTime.Text = "调出时间";
            else if (personInfo.workStatus == WorkStatusEnum.Fired)
                labeNCOretirementTime.Text = "除名时间";
            else
                labeNCOretirementTime.Text = "退休时间";

            //已退休，不分房
            if (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)
                personInfo.housingEligible = 0;

            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            PersonSharingInfo(personInfo);
        }
        private void ResetNCOInfoTab()
        {
            textBoxNCOName.Text = null;
            radioBtnNCOMale.Checked = false;
            radioBtnNCOFemale.Checked = false;

            comboBoxNCOUnit.Text = null;
            textBoNCOTechTitle.Text = null;
            textBoxNCOTechTitleTime.Text = null;
            textBoxNCOWorkTime.Text = null;
            textBoxNCOSignupTime.Text = null;
            textBoxNCORetirementTime.Text = null;

            comboBoxNCOOtherType.Text = null;
            textBoxNCOServiceLength.Text = null;
        }
        private void GetValueFromNCO(ref PersonInfo personInfo)
        {
            personInfo.name = textBoxNCOName.Text;

            if (radioBtnNCOMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxNCOUnit.Text;
            personInfo.techTitle = textBoNCOTechTitle.Text;
            personInfo.techTitleTime = this.dateProc.Parse(textBoxNCOTechTitleTime.Text);
            personInfo.workTime = this.dateProc.Parse(textBoxNCOWorkTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxNCOSignupTime.Text);
            personInfo.retirementTime = this.dateProc.Parse(textBoxNCORetirementTime.Text);

            WorkStatusEnum otherType;
            if (this.comboBoxNCOOtherType.SelectedItem == null)
            {
                otherType = WorkStatusEnum.Unknown;
            }
            else
            {
                otherType = DataDictionary.Instance().GetWorkStatus(this.comboBoxNCOOtherType.SelectedItem.ToString());
                personInfo.workStatus = otherType;
            }

            //没有的数据库字段设为空
            personInfo.adminDuty=null;
            personInfo.adminDutyTime=DateProc.InvalidDate();
            personInfo.techRank=null;
            personInfo.techRankTime=DateProc.InvalidDate();
            personInfo.curRankBeforeTime = DateProc.InvalidDate();
            personInfo.withArmy=0;
            personInfo.evaluation=null;

            //转业复原人员当年能分,离退休、调出、除名、遗属不分
            if ((personInfo.workStatus == WorkStatusEnum.Demobilization && (HousingDisDeadline.Instance().Deadline().Year - personInfo.retirementTime.Year != 0)) 
                || personInfo.workStatus == WorkStatusEnum.Fired || personInfo.workStatus == WorkStatusEnum.Martyred 
                || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Transferred)
            {
                personInfo.housingEligible = 0;
                checkBoxHousingEligible.Checked = false;
            }

            //退休不分房
            if (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)
            {
                personInfo.housingEligible = 0;
            }

            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;
            else
                personInfo.housingEligible = 0;

            GetPersonSharingInfo(ref personInfo);
        }


        //职员干部
        private void StaffInfo(PersonInfo personInfo)
        {
            textBoxStaffName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnStaffMale.Checked = true;
            else
                radioBtnStaffFemale.Checked = true;

            comboBoxStaffUnit.Text = personInfo.unit;
            comboBoxStaffAdminDuty.Text = personInfo.adminDuty;
            textBoxStaffAdminDutyTime.Text = DateTimeDisplayFormat(personInfo.adminDutyTime);
            comboBoxStaffTechTitle.Text = personInfo.techTitle;
            textBoxStaffTechTitleTime.Text = DateTimeDisplayFormat(personInfo.techTitleTime);
            textBoxStaffCurRankBeforeTime.Text = DateTimeDisplayFormat(personInfo.curRankBeforeTime);
            textBoxStaffWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxStaffSchoolTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxStaffSignupTime.Text = DateTimeDisplayFormat(personInfo.signupTime);

            string otherType = DataDictionary.Instance().GetWorkStatus(personInfo.workStatus);
            comboBoxStaffOtherType.Text = otherType;

            //离退休、调出、除名不分
            if (personInfo.workStatus == WorkStatusEnum.Transferred || personInfo.workStatus == WorkStatusEnum.Fired 
                || personInfo.workStatus == WorkStatusEnum.Retiree)
            {
                personInfo.housingEligible = 0;
            }

            int serviceLength = ServiceLengthCount.Instance().ServiceLength(personInfo);
            if (serviceLength < 0)
                serviceLength = 0;

            textBoxStaffServiceLength.Text = serviceLength.ToString();
           

            textBoxStaffRetirementTime.Text = DateTimeDisplayFormat(personInfo.retirementTime);

            //已退休，不分房
            if (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)
            {
                personInfo.housingEligible = 0;
            }

            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            PersonSharingInfo(personInfo);
        }
        private void ResetStaffInfoTab()
        {
            textBoxStaffName.Text = null;
            radioBtnStaffMale.Checked = false;
            radioBtnStaffFemale.Checked = false;

            comboBoxStaffUnit.Text = null;
            comboBoxStaffAdminDuty.Text = null;
            textBoxStaffAdminDutyTime.Text = null;
            comboBoxStaffTechTitle.Text = null;
            textBoxStaffTechTitleTime.Text = null;
            textBoxStaffCurRankBeforeTime.Text = null;
            textBoxStaffWorkTime.Text = null;
            textBoxStaffSchoolTime.Text = null;
            textBoxStaffSignupTime.Text = null;
            textBoxStaffRetirementTime.Text = null;

            comboBoxStaffOtherType.Text = null;
            textBoxStaffServiceLength.Text = null;
        }

        private void GetValueFromStaff(ref PersonInfo personInfo)
        {
            personInfo.name = textBoxStaffName.Text;

            if (radioBtnStaffMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxStaffUnit.Text;
            personInfo.adminDuty = comboBoxStaffAdminDuty.Text;
            personInfo.adminDutyTime = this.dateProc.Parse(textBoxStaffAdminDutyTime.Text);
            personInfo.techTitle = comboBoxStaffTechTitle.Text;
            personInfo.techTitleTime = this.dateProc.Parse(textBoxStaffTechTitleTime.Text);
            personInfo.curRankBeforeTime = this.dateProc.Parse(textBoxStaffCurRankBeforeTime.Text);
            personInfo.workTime = this.dateProc.Parse(textBoxStaffWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxStaffSchoolTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxStaffSignupTime.Text);

            personInfo.retirementTime = this.dateProc.Parse(textBoxStaffRetirementTime.Text);

            WorkStatusEnum otherType;
            if (this.comboBoxStaffOtherType.SelectedItem == null)
            {
                otherType = WorkStatusEnum.Unknown;
            }
            else
            {
                otherType = DataDictionary.Instance().GetWorkStatus(this.comboBoxStaffOtherType.SelectedItem.ToString());
                personInfo.workStatus = otherType;
            }

            personInfo.techRank=null;
            personInfo.techRankTime=DateProc.InvalidDate();
            personInfo.curRankBeforeTime=DateProc.InvalidDate();
            personInfo.retirementTime=DateProc.InvalidDate();
            personInfo.withArmy=0;
            personInfo.evaluation=null;

            //离退休、调出、除名不分
            if (personInfo.workStatus == WorkStatusEnum.Fired || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Transferred)
            {
                personInfo.housingEligible = 0;
                checkBoxHousingEligible.Checked = false;
            }

            //退休
            if (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)
            {
                personInfo.housingEligible = 0;
            }

            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;
            else
                personInfo.housingEligible = 0;

            GetPersonSharingInfo(ref personInfo);
        }

        //工人
        private void WorkerInfo(PersonInfo personInfo)
        {
            textBoxWorkerName.Text = personInfo.name;

            if (personInfo.sex == "男")
                radioBtnWorkerMale.Checked = true;
            else
                radioBtnWorkerFemale.Checked = true;

            comboBoxWorkerUnit.Text = personInfo.unit;
            comboBoxWorkerTechTitle.Text = personInfo.techTitle;
            textBoxWorkerWorkTime.Text = DateTimeDisplayFormat(personInfo.workTime);
            textBoxWorkerSchoolTime.Text = DateTimeDisplayFormat(personInfo.schoolTime);
            textBoxWorkerSignupTime.Text = DateTimeDisplayFormat(personInfo.signupTime);
            textBoxWorkerRetirementTime.Text = DateTimeDisplayFormat(personInfo.retirementTime);

            string otherType = DataDictionary.Instance().GetWorkStatus(personInfo.workStatus);
            comboBoxWorkerOtherType.Text = otherType;

            //离退休2、调出3、除名4不分
            if (personInfo.workStatus == WorkStatusEnum.Transferred || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Fired)
            {
                personInfo.housingEligible = 0;
            }

            int serviceLength = ServiceLengthCount.Instance().ServiceLength(personInfo);
            if (serviceLength < 0)
                serviceLength = 0;
            textBoxWorkerServicelength.Text = serviceLength.ToString();

            //已退休，不分房
            if (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)
            {
                personInfo.housingEligible = 0;
            }

            if (personInfo.housingEligible == 1)
                checkBoxHousingEligible.Checked = true;
            else
                checkBoxHousingEligible.Checked = false;

            PersonSharingInfo(personInfo);
        }
        private void ResetWorkerInfoTab()
        {
            textBoxWorkerName.Text = null;
            radioBtnWorkerMale.Checked = false;
            radioBtnWorkerFemale.Checked = false;

            comboBoxWorkerUnit.Text = null;
            comboBoxWorkerTechTitle.Text = null;
            textBoxWorkerWorkTime.Text = null;
            textBoxWorkerSchoolTime.Text = null;
            textBoxWorkerSignupTime.Text = null;
            textBoxWorkerRetirementTime.Text = null;
            textBoxWorkerServicelength.Text = null;
            
            comboBoxWorkerOtherType.Text = null;
        }
        private void GetValueFromWorker(ref PersonInfo personInfo)
        {
            personInfo.name = textBoxWorkerName.Text;

            if (radioBtnWorkerMale.Checked)
                personInfo.sex = "男";
            else
                personInfo.sex = "女";

            personInfo.unit = comboBoxWorkerUnit.Text;
            personInfo.techTitle = comboBoxWorkerTechTitle.Text;
            personInfo.workTime = this.dateProc.Parse(textBoxWorkerWorkTime.Text);
            personInfo.schoolTime = this.dateProc.Parse(textBoxWorkerSchoolTime.Text);
            personInfo.signupTime = this.dateProc.Parse(textBoxWorkerSignupTime.Text);
            personInfo.retirementTime = this.dateProc.Parse(textBoxWorkerRetirementTime.Text);

            WorkStatusEnum otherType;
            if (this.comboBoxWorkerOtherType.SelectedItem == null)
            {
                otherType = WorkStatusEnum.Unknown;
            }
            else
            {
                otherType = DataDictionary.Instance().GetWorkStatus(this.comboBoxWorkerOtherType.SelectedItem.ToString());
                personInfo.workStatus = otherType;
            }

            personInfo.adminDuty=null;
            personInfo.adminDutyTime=DateProc.InvalidDate();
            personInfo.techRank=null;
            personInfo.techRankTime=DateProc.InvalidDate();
            personInfo.techTitleTime=DateProc.InvalidDate();
            personInfo.curRankBeforeTime=DateProc.InvalidDate();
            personInfo.withArmy=0;
            personInfo.evaluation=null;

            //离退休、调出、除名不分
            if (personInfo.workStatus == WorkStatusEnum.Fired || personInfo.workStatus == WorkStatusEnum.Retiree || personInfo.workStatus == WorkStatusEnum.Transferred)
            {
                personInfo.housingEligible = 0;
                checkBoxHousingEligible.Checked = false;
            }

            //退休
            if (personInfo.retirementTime.CompareTo(DateProc.InvalidDate()) != 0)
                personInfo.housingEligible = 0;

            if (checkBoxHousingEligible.Checked)
                personInfo.housingEligible = 1;
            else
                personInfo.housingEligible = 0;

            GetPersonSharingInfo(ref personInfo);
        }

        #endregion

        #region 数据查询
        private int GetPersonCount()
        {
            StringBuilder sql = new StringBuilder("select count(*) from person where type ='");
            PersonTypeEnum type = GetCurPersonType();
            sql.Append((int)type);
            sql.Append("'");

            try
            {
                DataTable dt = DBProc.Instance().ReadData(sql.ToString()).Tables[0];

                return Convert.ToInt32(dt.Rows[0][0]);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        //当前数据查询条件下的数据总量
        private int personCount = 0;
        //存放查询结果的表
        private DataTable personTable = null;
        //当前人员信息
        PersonInfo curPerInfo;
        //表最大的容量
        private int MAX_COUNT = 100;

        //按 MAX_COUNT 为单位的偏移量
        private int offset = 0;
        //当前的数据的index [0, personCount-1]
        //curIndex / MAX_COUNT = offset
        private int curIndex = 0;

        private static string PERSON_QUERY_SQL_HEADER = "select person.* from person where 1=1 ";
        ObjectParser parser = new PersonParserFromMySql();
        private DataTable ReadPersonData(int offset)
        {
            StringBuilder sql = new StringBuilder(PERSON_QUERY_SQL_HEADER);
            sql.Append("and type ='");
            PersonTypeEnum type = GetCurPersonType();
            sql.Append((int)type);
            sql.Append("' order by idperson desc limit ");
            sql.Append(offset.ToString());
            sql.Append(", ");
            sql.Append(MAX_COUNT.ToString());

            return DBProc.Instance().ReadData(sql.ToString()).Tables[0];
        }
        #endregion

        private void ResetPersonDisplayState()
        {
            this.personCount = GetPersonCount();
            this.curIndex = 0;
            this.offset = 0;
        }
        
        #region 界面相关代码
        private void InitDGVCurHousing()
        {
            DGVInit.Init(ref this.dataGridViewCurHousing, DataDictionary.Instance().GetDGVConf(DataDictionary.Instance().dgvNames[3]));
        }

        private void CreateMaritalComboxItem(ComboBox cb)
        {
            string[] marital = DataDictionary.Instance().PersonMaritalStatus();
            for (int i = 0; i < marital.Length;i++ )
            {
                cb.Items.Add(marital[i]);
            }
        }

        private void CreateOthertypeComboxItem(ComboBox cb, PersonTypeEnum personType)
        {
            string[] workStatus = DataDictionary.Instance().WorkStatusGroup(personType);

            if (workStatus == null)
                return;

            for (int i = 0; i < workStatus.Length; i++)
            {
                cb.Items.Add(workStatus[i]);
            }
        }

        private void CreateWitharmyComboItem(ComboBox cb)
        {
            string[] withArmy = DataDictionary.Instance().WithArmyStatus();
            for (int i = 0; i < withArmy.Length; i++)
            {
                cb.Items.Add(withArmy[i]);
            }
        }

        private void CreateUnitComboxItem(ComboBox cb)
        {
            string[] unit = DataDictionary.Instance().Unit();

            for (int i = 0; i < unit.Length; i++)
            {
                cb.Items.Add(unit[i]);
            }
        }

        private void CreateTechRankComboxItem(ComboBox cb)
        {
            string[] techRank = DataDictionary.Instance().ArmmanTechRank();

            for (int i = 0; i < techRank.Length; i++)
            {
                cb.Items.Add(techRank[i]);
            }
        }

        private void CreateArmmanAdmindutyComboxItem(ComboBox cb)
        {
            string[] adminDuty = DataDictionary.Instance().Grade();

            for (int i = 0; i < adminDuty.Length; i++)
            {
                cb.Items.Add(adminDuty[i]);
            }
        }

        private void CreateArmmanEvaluationComboxItem(ComboBox cb)
        {
            string[] personEvaluation = DataDictionary.Instance().PersonEvaluation();
            for (int i = 0; i < personEvaluation.Length; i++)
                cb.Items.Add(personEvaluation[i]);
        }

        private void CreateStaffAdmindutyComboxItem(ComboBox cb)
        {
            string[] staffAdminduty = DataDictionary.Instance().StaffGrade();
            for (int i = 0; i < staffAdminduty.Length; i++)
            {
                cb.Items.Add(staffAdminduty[i]);
            }
        }

        private void CreateStaffTecktitleComboxItem(ComboBox cb)
        {
            string[] staffTechtitle = DataDictionary.Instance().StaffTechtitle();
            for (int i = 0; i < staffTechtitle.Length; i++)
                cb.Items.Add(staffTechtitle[i]);
        }

        private void CreateWorkerTechtitleComboxItem(ComboBox cb)
        {
            string[] workerTechtitle = DataDictionary.Instance().WorkerTechtitle();
            for (int i = 0; i < workerTechtitle.Length; i++)
                cb.Items.Add(workerTechtitle[i]);
        }

        private void CreateTechTitleComboxItem(ComboBox cb)
        {
            int i = 0;

            //高级职称
            string[] highTechTitle = DataDictionary.Instance().ArmmanHighTechTitle();

            for (; i < highTechTitle.Length; i++)
            {
                cb.Items.Add(highTechTitle[i]);
            }
            //非高级职称
            string[] midTechTitle = DataDictionary.Instance().ArmmanMidTechTitle();

            for (i = 0; i < midTechTitle.Length; i++)
            {
                cb.Items.Add(midTechTitle[i]);
            }
        }
        #endregion

        PersonTypeEnum GetCurPersonType()
        {
            string type = this.tabControl.TabPages[this.tabControl.SelectedIndex].Text;
            return DataDictionary.Instance().GetPersonType(type);
        }

        DateProc dateProc = new DateProc("YY-MM-DD");
        private DataStruct2SQL sqlCreator = new DataStruct2SQL();
    }
}
